local catapultSkill = fk.CreateSkill {
  name = "#catapult_skill",
  attached_equip = "catapult",
}

catapultSkill:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(catapultSkill.name)
  end,
  on_cost = function(self, event, target, player, data)
    local room =player.room
    local targets = {}
    for _, p in ipairs(player.room:getOtherPlayers(player, false)) do
      if p:getHandcardNum() > player:getHandcardNum() then
        table.insert(targets, p)
      end
    end
    if #targets == 0 then return end
    if #targets > 0 then
      event:setCostData(self, {tos = targets})
      return true
    end  
  end,  
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).tos
    if #targets == 0 then return end
    if #targets > 0 then
    room:sortByAction(targets)
      for _, p in ipairs(room:getAllPlayers()) do
        if table.contains(targets, p) then
          room:doIndicate(player, p)    
        end
      end
    for _, p in ipairs(targets) do
        if not p.dead and not p:isKongcheng() then
          room:askToDiscard(p, {
            min_num = 1,
            max_num = 1,
            include_equip = false,
            skill_name = catapultSkill.name,
            cancelable = false,
          })
        end
      end
    end
  end,
})

return catapultSkill

